package com.time.hellotime.components.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.time.hellotime.R;
import com.time.hellotime.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bing on 2016/8/31.
 * E-mail is tx16bing@163.com
 */
public class GuideActivity extends BaseActivity {

    private ViewPager vpGuidePage;
    private LinearLayout llIndicator;
    private ImageView ivOne;
    private ImageView ivTwo;
    private ImageView ivThree;

    private GuideOneFragment oneFragment;
    private GuideTwoFragment twoFragment;
    private GuideThreeFragment threeFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        assignViews();
        setupViews();
    }

    @Override
    public boolean needTranslucentStatus() {
        return true;
    }

    private void assignViews() {
        vpGuidePage = findView(R.id.view_pager);
        llIndicator = findView(R.id.ll_indicator);
        ivOne = findView(R.id.iv_one);
        ivTwo = findView(R.id.iv_two);
        ivThree = findView(R.id.iv_three);

    }

    private void setupViews() {
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        oneFragment = new GuideOneFragment();
        twoFragment = new GuideTwoFragment();
        threeFragment = new GuideThreeFragment();
        fragments.add(oneFragment);
        fragments.add(twoFragment);
        fragments.add(threeFragment);

        vpGuidePage.setAdapter(new ViewPagerAdapter(fragments));
        vpGuidePage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        llIndicator.setVisibility(View.VISIBLE);
                        setIndicatorBg(R.drawable.indicator_select, R.drawable.indicator_unselect,
                                R.drawable.indicator_unselect);
                        break;

                    case 1:
                        llIndicator.setVisibility(View.VISIBLE);
                        setIndicatorBg(R.drawable.indicator_unselect, R.drawable.indicator_select,
                                R.drawable.indicator_unselect);
                        break;

                    case 2:
                        setIndicatorBg(R.drawable.indicator_unselect, R.drawable.indicator_unselect,
                                R.drawable.indicator_select);
                        llIndicator.setVisibility(View.GONE);
                        break;

                    case 3:
//                        btnGo.setVisibility(View.VISIBLE);
//                        setIndicatorBg(R.drawable.indicator_unselect, R.drawable.indicator_unselect,
//                                R.drawable.indicator_unselect, R.drawable.indicator_select);

                        break;

                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setIndicatorBg(int one, int two, int three) {
        ivOne.setBackgroundResource(one);
        ivTwo.setBackgroundResource(two);
        ivThree.setBackgroundResource(three);
    }

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> fragments = new ArrayList<Fragment>();

        public ViewPagerAdapter(List<Fragment> listFragments) {
            super(getSupportFragmentManager());
            this.fragments = listFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
